﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace BHair.Base
{
    public partial class frmhyzk : Form
    {
        public frmhyzk()
        {
            InitializeComponent();
        }
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        private void button1_Click(object sender, EventArgs e)
        {
            string num;
            string kahao;
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            //判断卡号位数
            if (comboBox1.Text == "1")
            {
                for (int i = 0; i < Int32.Parse(textBox2.Text); i++)
                {
                    num = textBox1.Text + (i + 1).ToString("0");
                    //kahao = num.ToString("0000");
                    listBox1.Items.Add(num);
                }
            }
            else if (comboBox1.Text == "2")
            {
                for (int i = 0; i < Int32.Parse(textBox2.Text); i++)
                {
                    num = textBox1.Text + (i + 1).ToString("00");
                    //kahao = num.ToString("0000");
                    listBox1.Items.Add(num);
                }
            }
            else if (comboBox1.Text == "3")
            {
                for (int i = 0; i < Int32.Parse(textBox2.Text); i++)
                {
                    num = textBox1.Text + (i + 1).ToString("000");
                    //kahao = num.ToString("0000");
                    listBox1.Items.Add(num);
                }
            }
            else if (comboBox1.Text == "4")
            {
                for (int i = 0; i < Int32.Parse(textBox2.Text); i++)
                {
                    num = textBox1.Text + (i + 1).ToString("0000");
                    //kahao = num.ToString("0000");
                    listBox1.Items.Add(num);
                }
            }
            else if (comboBox1.Text == "5")
            {
                for (int i = 0; i < Int32.Parse(textBox2.Text); i++)
                {
                    num = textBox1.Text + (i + 1).ToString("00000");
                    //kahao = num.ToString("0000");
                    listBox1.Items.Add(num);
                }
            }
            else if (comboBox1.Text == "6")
            {
                for (int i = 0; i < Int32.Parse(textBox2.Text); i++)
                {
                    num = textBox1.Text + (i + 1).ToString("000000");
                    //kahao = num.ToString("0000");
                    listBox1.Items.Add(num);
                }
            }
            else if (comboBox1.Text == "7")
            {
                for (int i = 0; i < Int32.Parse(textBox2.Text); i++)
                {
                    num = textBox1.Text + (i + 1).ToString("0000000");
                    //kahao = num.ToString("0000");
                    listBox1.Items.Add(num);
                }
            }
            else if (comboBox1.Text == "8")
            {
                for (int i = 0; i < Int32.Parse(textBox2.Text); i++)
                {
                    num = textBox1.Text + (i + 1).ToString("00000000");
                    //kahao = num.ToString("0000");
                    listBox1.Items.Add(num);
                }
            }
            else if (comboBox1.Text == "9")
            {
                for (int i = 0; i < Int32.Parse(textBox2.Text); i++)
                {
                    num = textBox1.Text + (i + 1).ToString("000000000");
                    //kahao = num.ToString("0000");
                    listBox1.Items.Add(num);
                }
            
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string num;
            string kahao;
            int flid=0;
            SqlConnection conn1 = new SqlConnection(constr);
            conn1.Open();
            string sql1 =string.Format( "SELECT cid, cname FROM card where cname='{0}'",comboBox2.Text);
            SqlCommand comm1 = new SqlCommand(sql1,conn1);
            SqlDataReader sdr = comm1.ExecuteReader();
            while (sdr.Read())
            {
                flid =Int32.Parse( sdr["cid"].ToString());
            }
            
            try
            {
                SqlConnection conn = new SqlConnection(constr);
                conn.Open();
                //判断卡号位数
                if (comboBox1.Text == "1")
                {
                    for (int i = 0; i < Int32.Parse(textBox2.Text); i++)
                    {
                        num = textBox1.Text + (i + 1).ToString("0");
                        //kahao = num.ToString("0000");
                        //listBox1.Items.Add(num);
                        string sql = string.Format("INSERT INTO 制卡 (card, 可用,fl) VALUES ('{0}',1,{1})", num,flid);
                        SqlCommand comm = new SqlCommand(sql, conn);
                        comm.ExecuteNonQuery();
                    }
                }
                else if (comboBox1.Text == "2")
                {
                    for (int i = 0; i < Int32.Parse(textBox2.Text); i++)
                    {
                        num = textBox1.Text + (i + 1).ToString("00");
                        //kahao = num.ToString("0000");
                        string sql = string.Format("INSERT INTO 制卡 (card, 可用,fl) VALUES ('{0}',1,{1})", num, flid);
                        SqlCommand comm = new SqlCommand(sql, conn);
                        comm.ExecuteNonQuery();
                    }
                }
                else if (comboBox1.Text == "3")
                {
                    for (int i = 0; i < Int32.Parse(textBox2.Text); i++)
                    {
                        num = textBox1.Text + (i + 1).ToString("000");
                        //kahao = num.ToString("0000");
                        string sql = string.Format("INSERT INTO 制卡 (card, 可用,fl) VALUES ('{0}',1,{1})", num, flid);
                        SqlCommand comm = new SqlCommand(sql, conn);
                        comm.ExecuteNonQuery();
                    }
                }
                else if (comboBox1.Text == "4")
                {
                    for (int i = 0; i < Int32.Parse(textBox2.Text); i++)
                    {
                        num = textBox1.Text + (i + 1).ToString("0000");
                        //kahao = num.ToString("0000");
                        string sql = string.Format("INSERT INTO 制卡 (card, 可用,fl) VALUES ('{0}',1,{1})", num, flid);
                        SqlCommand comm = new SqlCommand(sql, conn);
                        comm.ExecuteNonQuery();
                    }
                }
                else if (comboBox1.Text == "5")
                {
                    for (int i = 0; i < Int32.Parse(textBox2.Text); i++)
                    {
                        num = textBox1.Text + (i + 1).ToString("00000");
                        //kahao = num.ToString("0000");
                        string sql = string.Format("INSERT INTO 制卡 (card, 可用,fl) VALUES ('{0}',1,{1})", num, flid);
                        SqlCommand comm = new SqlCommand(sql, conn);
                        comm.ExecuteNonQuery();
                    }
                }
                else if (comboBox1.Text == "6")
                {
                    for (int i = 0; i < Int32.Parse(textBox2.Text); i++)
                    {
                        num = textBox1.Text + (i + 1).ToString("000000");
                        //kahao = num.ToString("0000");
                        string sql = string.Format("INSERT INTO 制卡 (card, 可用,fl) VALUES ('{0}',1,{1})", num, flid);
                        SqlCommand comm = new SqlCommand(sql, conn);
                        comm.ExecuteNonQuery();
                    }
                }
                else if (comboBox1.Text == "7")
                {
                    for (int i = 0; i < Int32.Parse(textBox2.Text); i++)
                    {
                        num = textBox1.Text + (i + 1).ToString("0000000");
                        //kahao = num.ToString("0000");
                        string sql = string.Format("INSERT INTO 制卡 (card, 可用,fl) VALUES ('{0}',1,{1})", num, flid);
                        SqlCommand comm = new SqlCommand(sql, conn);
                        comm.ExecuteNonQuery();
                    }
                }
                else if (comboBox1.Text == "8")
                {
                    for (int i = 0; i < Int32.Parse(textBox2.Text); i++)
                    {
                        num = textBox1.Text + (i + 1).ToString("00000000");
                        //kahao = num.ToString("0000");
                        string sql = string.Format("INSERT INTO 制卡 (card, 可用,fl) VALUES ('{0}',1,{1})", num, flid);
                        SqlCommand comm = new SqlCommand(sql, conn);
                        comm.ExecuteNonQuery();
                    }
                }
                else if (comboBox1.Text == "9")
                {
                    for (int i = 0; i < Int32.Parse(textBox2.Text); i++)
                    {
                        num = textBox1.Text + (i + 1).ToString("000000000");
                        //kahao = num.ToString("0000");
                        string sql = string.Format("INSERT INTO 制卡 (card, 可用,fl) VALUES ('{0}',1,{1})", num, flid);
                        SqlCommand comm = new SqlCommand(sql, conn);
                        comm.ExecuteNonQuery();
                    }

                }
                MessageBox.Show("制卡成功");
                conn.Close();
            }
            catch
            {
                MessageBox.Show("卡号不可重复添加！");
            }

            }

        private void frmhyzk_Load(object sender, EventArgs e)
        {
            //MessageBox.Show("ass");
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = "SELECT cid, cname FROM card";
            
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            DataSet ds = new DataSet();
            sda.Fill(ds, "card");
            comboBox2.DataSource = ds.Tables["card"];
            comboBox2.DisplayMember = "cname";
            conn.Close();
        }
    }
}
